package tt.dz.egobus.api.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tt.dz.egobus.api.exception.ResponseException;
import tt.dz.egobus.bean.I18NCodeEnum;
import tt.dz.egobus.servicemanager.facade.interator.FacadeFactory;

/**
 * @author Anson
 * @date 17-3-21
 * @name 扩展公共服务方法
 * @since 1.0.2
 */
public abstract class AbstractFacadeFactoryService extends CommonApiService {

    private static Logger LOGGER = LoggerFactory.getLogger(AbstractFacadeFactoryService.class);

    /**
     * @author Anson
     * @date 17-3-21
     * @name 获取当前facadeFacotry
     * @since 1.0.1
     */
    protected abstract FacadeFactory getFacadeFactory();

    /**
     * 获取当前facade
     *
     * @param facadeName
     * @param clazz
     * @param <T>
     * @return
     */
    protected <T> T facade(String facadeName, Class<T> clazz) throws ResponseException {
        T t = this.getFacadeFactory().getBean(facadeName, clazz);
        if (t == null) {
            LOGGER.error("获取当前facade为空，class={},facadeName={}", clazz, facadeName);
            throw new ResponseException(I18NCodeEnum.RPC_SYSTEM_CODE_ERROR);
        }
        return t;
    }

}
